﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using System.Data;
using MaxLeaf.DB;


namespace MaxLeaf.DAL
{
    public class arCustomerDALBuild : BaseDALBuilder
    {
        public arCustomerDALBuild()
            : base()
        {
            K3DAL = new K3.arCustomerDAL();
            WebDAL = null;// new Web.arCustomerDAL();
        }
        protected override bool ValidCheckOrgData(ServerInfo orgServer, DataRowView drvLog)
        {
            if ( orgServer.Type!= ServerType.Web )
            {
                //web分发
                return false;
            }
             

            return base.ValidCheckOrgData(orgServer, drvLog);
        }

        protected override string GetK3DestServerDBName(ServerInfo orgServer, DataRowView drvLog)
        {
            if ((LogStatus)CvtFunc.ToInt(drvLog[DEL.LogDEL.LogStatus]) == LogStatus.Delete)
            {
                return base.GetK3DestServerDBName(orgServer, drvLog);
            }

            if (orgServer.Type == ServerType.K3 && orgServer.MasterDB && orgServer.CityDBs.Count > 0)
            {
                using (DBAccess dbOrgCon = new DBAccess(orgServer))
                {
                    DataRow drOrgData = dbOrgCon.GetTableDataFirstRow("t_Organization", new Dictionary<string, object>() { { "FitemID", drvLog[DEL.LogDEL.KeyValue] } }, "FNumber");
                    string strFNumber = drOrgData["FNumber"].ToString();
                    var arrFNumber = strFNumber.Split('.');
                    for(int i=arrFNumber.Length;i>=1;i++)
                    {
                        DataRow drDbName = dbOrgCon.GetTableDataFirstRow("OrganizationDBMap", new Dictionary<string, object>() { { "FNumber",string.Join(".",arrFNumber.Take(i).ToArray())  } }, "DBName");
                        if (drDbName != null)
                        {
                            string strDbName = CvtFunc.ToString(drDbName["DBName"]);
                            if (strDbName.Length == 0)
                            {
                                throw new MaxLeafException(string.Format("OrganizationDBMap 不存在 DBName for FNumber {0}", string.Join(".", arrFNumber.Take(i).ToArray())));
                            }
                            if (strDbName.Equals("mary", StringComparison.InvariantCultureIgnoreCase))
                            {
                                return orgServer.Database;
                            }
                            string strDestDBName = string.Empty;
                            foreach (string strCityDB in orgServer.CityDBs)
                            {
                                if (strDbName.Equals(strCityDB, StringComparison.InvariantCultureIgnoreCase))
                                {
                                    strDestDBName = strCityDB;
                                    break;
                                }
                            }
                            if (strDestDBName.Length == 0)
                            {
                                throw new MaxLeafException(string.Format("DBName {1} 无效 in OrganizationDBMap  for FNumber {0}", string.Join(".", arrFNumber.Take(i).ToArray()), strDbName));
                            }

                            return strDestDBName;
                        }
                    }
                }
            }

            return base.GetK3DestServerDBName(orgServer, drvLog);
        }
    }
}
